package com.xiaoshangkou.hellopra.controller.backstage;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoshangkou.hellopra.bean.Permission2WithStatus;
import com.xiaoshangkou.hellopra.bean.PermissionWithStatus;
import com.xiaoshangkou.hellopra.pojo.Role;
import com.xiaoshangkou.hellopra.pojo.UserGroup;
import com.xiaoshangkou.hellopra.service.UserGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/backstage/userGroup")
public class UserGroupController {
    @Autowired
    private UserGroupService userGroupService;

    /**
     * 用途：进入用户组页面时分页查询所有用户组
     * @RequestParam :设置默认值
     * page:当前页的页码（current）
     * size:每页显示的条数（size）
     */
    @RequestMapping("/all")
    public ModelAndView all(@RequestParam(defaultValue = "1") int page,
                            @RequestParam(defaultValue = "5") int size){

        ModelAndView modelAndView=new ModelAndView();
        Page<UserGroup> userGroupPage = userGroupService.findPage(page, size);
        modelAndView.addObject("userGroupPage",userGroupPage);
        modelAndView.setViewName("/backstage/usergroup_all");
        return  modelAndView;
    }

    /**
     * 用途：添加新的用户组
     * userGroup :前端传来的用户组对象
     * 待更新：后期前端页面通过json数据实现局部更新
     */
    @RequestMapping("/add")
    @ResponseBody//不会跳转页面,返回字符串（json数据）
    public String add(UserGroup userGroup){
        userGroupService.add(userGroup);
        return "添加成功";
    }

    /**
     * 用途：删除角色
     * @param uid 前端传来的要删除用户组的uid
     * @return
     */
    @RequestMapping("/delete")
    public String delete(Integer uid){
        userGroupService.delete(uid);
        return "redirect:/backstage/userGroup/all";
    }


    /**
     *用途：在修改用户组之前，先将原来的数据返回到前端
     *uid:角色的uid
     */
    @RequestMapping("/edit")
    @ResponseBody
    public UserGroup edit(Integer uid){
        return userGroupService.findById(uid);
    }

    /**
     * 用途：修改角色
     * userGroup :前端传来的用户组对象
     * 待更新：后期前端页面通过json数据实现局部更新
     */
    @RequestMapping("/update")
    @ResponseBody
    public String update(UserGroup userGroup){
        userGroupService.update(userGroup);
        return "修改成功";
    }


    /**
     * 用途:用户点击分配权限时，跳转到分配页显示所有权限
     * 用户组拥有的权限，默认打上勾
     * @param uid 用户组aid
     * uid传入userGroup_permission是为了分配完权限后，提交到后端，后端知道是谁
     */
    @RequestMapping("/findPermission2")
    public ModelAndView findPermission2(Integer uid){
        ModelAndView modelAndView=new ModelAndView();
        List<Permission2WithStatus> permissions = userGroupService.findPermission(uid);
        modelAndView.addObject("permissions",permissions);
        modelAndView.addObject("uid",uid);
        modelAndView.setViewName("/backstage/userGroup_permission2");
        return modelAndView;
    }

    @RequestMapping("/updatePermission2")
    public String updatePermission(Integer[] ids,Integer uid){
        userGroupService.updatePermissioon2(ids,uid);
        return "redirect:/backstage/userGroup/all";
    }



}
